<!DOCTYPE html>
<html>
<head> <@h.head />
<link href="${path }/static/plugins/zTree/css/zTreeStyle/zTreeStyle.css"
	rel="stylesheet" type="text/css" />
<script type="text/javascript"
	src="${path}/static/plugins/zTree/js/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript"
	src="${path}/static/plugins/zTree/js/jquery.ztree.excheck-3.5.min.js"></script>
	
</head>
<body>
	<ul id="rightTree" class="ztree"></ul>
	<script type="text/javascript">
		var api = frameElement.api, W = api.opener;
		api.button({
		    id:'valueOk',
		    name:'确定',
		    callback: saveRight
		});
		
		var setting = {
			check: {enable: true},
			data: {
				simpleData: {enable: true}
			}
		};
		$(function() {
			var url = "${path}/sysrole/getRight/" + ${(roleId)!''};
			$.get(url, function(data) {
				var data = eval('(' + data + ')');
				if(data.msg != null){
					alert(data.msg);
					return false;
				}
				$.fn.zTree.init($("#rightTree"), setting, data.data);
				
				var treeObj = $.fn.zTree.getZTreeObj("rightTree"); 
				treeObj.expandAll(true); 
				
			});
			
		});
		
		function saveRight(){
			var treeObj = $.fn.zTree.getZTreeObj("rightTree");
			var sNodes = treeObj.getCheckedNodes();
			var len = sNodes.length;
			if (len > 0) {
				var sNodeIds = "";
				for(var i = 0; i < len; i++){
					sNodeIds = sNodeIds + sNodes[i].id + ",";
				}
				var url = "${path}/sysrole/saveRight/" + ${(roleId)!''};
				$.ajax({
					type : "post",
					url : url,
					data : "treeids=" + sNodeIds,
					async : false,
					success : function(data){
						data = eval("(" + data + ")");
						if(data.msg != null && data.msg != ''){
							alert(data.msg);
							return false;
						}
						if(data.status == 200){
							alert('保存成功');
						}else{
							alert('保存失败');
						}
					}
				});
			}else{
				alert('至少选择一个叶子节点');
			}
		}
	</script>
	
</body>
</html>